package com.tianqu.tms.www.car.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.MarketCarInfoDto;
import com.tianqu.tms.dto.MarketCarServiceDto;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.MarketCarGateInfo;
import com.tianqu.tms.entity.MarketCarService;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.www.car.actionform.CarPendingLiftActionForm;
import com.tianqu.tms.www.car.viewhelper.CarPendingLiftViewHelper;

/**
 * @author mufeiyu
 * 库存移库 - 信息查看 controller
 */
public class CarStockTransferConfirmController extends TmsController {
	private Logger logger = Logger.getLogger(CarStockTransferConfirmController.class);
	public final static int CARCOLORITEMID = 105;
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			CarPendingLiftViewHelper viewhelper = new CarPendingLiftViewHelper();
			CarPendingLiftActionForm form = (CarPendingLiftActionForm)uc.getInputForm();
			String vin = form.getVin();
			String id = form.getId();
			String licensePlate = form.getLicensePlate();
			
			Car car = Car.getInstance();
			List<MarketCarInfoDto> list = new ArrayList<MarketCarInfoDto>();
			if(!StringUtil.isNullOrBlank(licensePlate)){
				list = car.findCarList(licensePlate, 2);
			}else if(!StringUtil.isNullOrBlank(vin)){
				list = car.findCarList(vin, 1);
			}else if(!StringUtil.isNullOrBlank(id)){
				list = car.findCarList(id, 3);
			}
			
			//查询结果判断
			if (null == list || list.size() == 0) {
				// 没有查询到数据
				viewhelper.setConfirmFlag(1);
				uc.setAttribute(TmsConstants.VIEWHELP, viewhelper);
			}else if(list.size() == 1){
				MarketCarInfoDto infoDto = list.get(0);
				MarketCarServiceDto dto = car.findMarketCarService(infoDto.getSerial());
				MarketCarService carService = MarketCarService.getInstance();
				MarketCarService marketCarService =  carService.findCarServiceInfoBySerial(infoDto.getSerial());
				viewhelper.setCheckPrice(marketCarService.getCheckPrice());
				viewhelper.setPayment(String.valueOf(dto.getTransferPrice()));
				
				//查询到一条数据
				viewhelper.setConfirmFlag(2);
				setDetailInfo(list.get(0),viewhelper);
				uc.setAttribute(TmsConstants.VIEWHELP, viewhelper);
			} else {
				// 查询到多条数据
				int i = 0;
				for (MarketCarInfoDto dto : list) {
					dto.setNum(++i);
					if(i%2 == 0) dto.setCssFlag(2); 
					else dto.setCssFlag(1);					
				}
				viewhelper.setConfirmFlag(3);
				viewhelper.setMarkCarInfoDtoList(list);
				uc.setAttribute(TmsConstants.VIEWHELP, viewhelper);
			}
			 			
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
	}

	/**
	 * 信息查看页面表示数据设定
	 * @param car
	 * @param viewhelper
	 * @throws TmsDaoException
	 */
	private void setDetailInfo(MarketCarInfoDto car,CarPendingLiftViewHelper viewhelper) throws TmsDaoException{
		
		// 基本信息设定
		// 车辆编号
		viewhelper.setSerial(car.getSerial());
		viewhelper.setBasicCarId(car.getSerial());

		// 车牌号码
		viewhelper.setLicensePlate(car.getLicensePlate());

		// VIN码
		viewhelper.setVin(car.getVin());

		// 车辆名称
		viewhelper.setCarName(car.getCarName());

        // 库存状态
		viewhelper.setRegisterStatusNum(car.getRegisterStatus());
		switch (car.getRegisterStatus()) {
			case 1: viewhelper.setRegisterStatus("已入库");
			break;
			case 2: viewhelper.setRegisterStatus("已出库");
			break;
			case 3: viewhelper.setRegisterStatus("过期");
			break;
			case 4: viewhelper.setRegisterStatus("存档");
			break;
			case 6: viewhelper.setRegisterStatus("登记");
			break;
		}

		//字典信息
		Codes code = Codes.getInstance();
        // 车身顏色
		viewhelper.setCarColor(code.getItemValueByItemIdAndItemCode(CARCOLORITEMID,car.getColor()));
		
        // 当前商户ID
		viewhelper.setActiveMemberId(car.getMemberId());
		
		// 迁移到商户
		Member member = Member.getInstance();
		member = member.findCarScoreByMemberId(String.valueOf(car.getMemberId()));
		//商户信息	
		if(member != null){			
			// 当前库存量
			viewhelper.setUsedCarPort(member.getUsedCarPort());
			// 车位总数
			viewhelper.setCarPort(member.getCarPort());
			// 商户名称
			viewhelper.setMemName(member.getMemName());
		}
		
		//库存移库确认按钮
		viewhelper.setButtonFlag("true");		
		
		//车辆进出记录（最近5次）
		MarketCarGateInfo marketCarGateInfo = MarketCarGateInfo.getInstance();
		int i=1;
		List<MarketCarGateInfo> marketCarGateInfoList = new ArrayList<MarketCarGateInfo>();
		for(MarketCarGateInfo info : marketCarGateInfo.findAllGateInfoBySerial(car.getSerial())){
		   if(i>5) break;
		   if(i%2 == 0) info.setCssFlag(2); 
		   else info.setCssFlag(1);
		   info.setNum(i);
		   i++;
		   marketCarGateInfoList.add(info);
		}
		viewhelper.setGateInfoList(marketCarGateInfoList);
	}
}
